package com.zzsy.carosshopprojects.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.view.CusWebView;

import androidx.annotation.NonNull;


/**
 * Created by Administrator on 2019/12/2 0002.
 */

public class ComTwoBtnDialog extends Dialog implements View.OnClickListener {


    public static final int DIALOG_CLEAR_CACHE = 1;
    public static final int DIALOG_CACHE_DATA = 2;
    public static final int DIALOG_VERSION_UPDATES = 3;
    public static final int DIALOG_CALL_PHONE = 4;
    public static final int DIALOG_CLEAR_MY_CACHE = 6;
    public static final int DIALOG_DO_HANDOVER = 7;
    public static final int RECEIPT_LIST = 8;
    public static final int BACK_HOME = 9;
    public static final int BANBALCE_PAY = 10;
    public static final int JIE_BANG_CARD = 11;
    public static final int GUA_SHI_CARD = 12;
    public static final int BUKA_CARD = 13;
    public static final int DEL_COUPON = 14;

    private Activity activity;

    private TextView titleTv;
    private TextView leftBtn;
    private TextView rightBtn;

    private String titleStr;

    private String leftBtnStr;
    private int leftTxtColor;
    private Drawable leftBtnColor;

    private String rightBtnStr;
    private int rightTxtColor;
    private Drawable rightBtnColor;
    private CusWebView webView;
    private String content;
    private IDialogBtnClickListener dialogBtnClickListener;
    private int type=1;

    public interface IDialogBtnClickListener {
        void onDialogLeftBtnClick();

        void onDialogRightBtnClick();
    }

    public ComTwoBtnDialog(@NonNull Activity context, int dialogType, String contText) {
        this(context, R.style.BottomPopupDialog, dialogType,contText);
    }

    public ComTwoBtnDialog(@NonNull Activity context, int themeResId, int dialogType, String contText) {
        super(context, themeResId);

        this.activity = context;
        this.content = contText;
        Resources res = activity.getResources();
        switch (dialogType) {
            case DIALOG_CLEAR_CACHE:
                titleStr = "是否退出当前账号？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);

                break;
            case DIALOG_CACHE_DATA:
                titleStr = "是否清除缓存？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.xu_hua_xian);

                break;

            case DIALOG_VERSION_UPDATES:
                titleStr = "有新版本，是否进行更新！\n" +content;
                type =2;
                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.xu_hua_xian);

                break;

            case DIALOG_CALL_PHONE:
                titleStr = "400-1078-8988";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "呼叫";
                rightTxtColor = res.getColor(R.color.xu_hua_xian);

                break;
            case DIALOG_CLEAR_MY_CACHE:
                titleStr = "是否确认核销？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.xu_hua_xian);

                break;

            case DIALOG_DO_HANDOVER:
                titleStr = "是否确认交接班？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;

            case RECEIPT_LIST:
                titleStr = "是否确认额度统计？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;
            case BACK_HOME:
                titleStr = "支付超时？";

                leftBtnStr = "继续等待";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "返回首页";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;
            case BANBALCE_PAY:
                titleStr = "是否确认现金充值？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;
            case JIE_BANG_CARD:
                titleStr = "确定解绑副卡？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;
            case GUA_SHI_CARD:
                    titleStr = "确定挂失副卡？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;
            case BUKA_CARD:
                    titleStr = "该会员存在,是否补实体卡？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;
            case DEL_COUPON:
                titleStr = "是否删除红包？";

                leftBtnStr = "取消";
                leftTxtColor = res.getColor(R.color.black);

                rightBtnStr = "确定";
                rightTxtColor = res.getColor(R.color.end_color_jb);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setContentView(R.layout.dialog_com_two_btn);
        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        initUI();
    }

    private void initUI() {
        titleTv = findViewById(R.id.tip_tv);
        leftBtn = findViewById(R.id.left_btn);
        rightBtn = findViewById(R.id.right_btn);
        webView = findViewById(R.id.cusview);
        titleTv.setOnClickListener(this);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        if(type ==2){
            webView.setVisibility(View.VISIBLE);
            titleTv.setVisibility(View.GONE);
            webView.loadHtmlString(titleStr);
        }else{
            webView.setVisibility(View.GONE);
            titleTv.setVisibility(View.VISIBLE);
            titleTv.setText(titleStr);
        }
        setLeftBtnStyles(true, leftBtnStr, leftTxtColor, leftBtnColor);
        setRightBtnStyles(true, rightBtnStr, rightTxtColor, rightBtnColor);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                leftBtn.setClickable(false);
                leftBtn.setFocusable(false);
                dialogBtnClickListener.onDialogLeftBtnClick();
                dismiss();
                break;

            case R.id.right_btn:
                rightBtn.setClickable(false);
                rightBtn.setFocusable(false);
                dialogBtnClickListener.onDialogRightBtnClick();
                dismiss();
                break;

            default:
                break;
        }
    }

//    @Override
//    public void show() {
//        super.show();
////        Window window = getWindow();
////        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
////        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
////        layoutParams.alpha = 0.5f;
////        layoutParams.dimAmount = 0.5f;
////        activity.getWindow().setAttributes(layoutParams);
//
//    }

//    @Override
//    public void dismiss() {
//        super.dismiss();
//
////        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
////        layoutParams.alpha = 1.0f;
////        layoutParams.dimAmount = 1.0f;
////        activity.getWindow().setAttributes(layoutParams);
//    }

    public void setTitleContent(String titleStr) {
        if (titleTv != null) {
            titleTv.setText(titleStr);
        }
    }

    public void setLeftBtnStyles(boolean needShow, String tipStr, int txtColorId, Drawable btnColorId) {
        if (leftBtn != null) {
            leftBtn.setVisibility(needShow ? View.VISIBLE : View.GONE);
            leftBtn.setText(tipStr);
            leftBtn.setTextColor(txtColorId);
            leftBtn.setBackground(btnColorId);
        }
    }

    public void setRightBtnStyles(boolean needShow, String tipStr, int txtColorId, Drawable btnColorId) {
        if (rightBtn != null) {
            rightBtn.setVisibility(needShow ? View.VISIBLE : View.GONE);
            rightBtn.setText(tipStr);
            rightBtn.setTextColor(txtColorId);
            rightBtn.setBackground(btnColorId);
        }
    }

    public void setDialogBtnClickListener(IDialogBtnClickListener dialogBtnClickListener) {
        this.dialogBtnClickListener = dialogBtnClickListener;
    }
}
